﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class DepartmentalManager
    {
        public DepartmentalInfo GetDepartmentalById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryDepartmental = from d in db.Departmentals
                                    where d.Id.Equals(id)
                                    select new DepartmentalInfo { Id = d.Id,  Name = d.Name,Description=d.Description};

                return queryDepartmental.First();
            }
        
        }


        public IList<DepartmentalInfo> ListDepartmental()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryDepartmental = from d in db.Departmentals
                                   select new DepartmentalInfo { Id = d.Id,Name = d.Name,Description=d.Description };

                return queryDepartmental.ToList();
            }
        }
    

        public void CreateDepartmental(Departmental Departmental)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Departmentals.InsertOnSubmit(Departmental);

                db.SubmitChanges();
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Departmental"></param>
        public void UpdateDepartmental(Departmental Departmental)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Departmental = db.Departmentals.Single(d => d.Id.Equals(Departmental.Id));

                _Departmental.Name = Departmental.Name;
                _Departmental.Description = Departmental.Description;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteDepartmental(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Departmental = db.Departmentals.Single(d => d.Id.Equals(id));

                db.Departmentals.DeleteOnSubmit(_Departmental);
                db.SubmitChanges();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasNameExisted(string name)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Departmentals
                            where d.Name.Equals(name)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasNameDuplicated(string name, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Departmentals
                            where d.Name.Equals(name)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

     
    }
}
